/**
 * File  : Mutex.h
 * Time  : 2014-12-17 10:00
 * Author: KashingLee
 * Ref   : mooon
 */
#ifndef PLCC_THREAD_MUTEX_H_
#define PLCC_THREAD_MUTEX_H_
#include <pthread.h>
#include "ThreadException.h"

class Mutex {
 friend class Event;

 public:
  Mutex();
  ~Mutex();
  
  void Lock();
  void Unlock();
  bool TryLock();
  
 private:
  pthread_mutex_t     mutex_;
  pthread_mutexattr_t attr_;
};

template <class ClassMutex>
class AutoMutex {
 public:
  AutoMutex(ClassMutex &mutex): mutex_(mutex) {
    mutex_.Lock();
  }
  ~AutoMutex() {
    mutex_.Unlock();
  }

 private:
  AutoMutex() {};
  ClassMutex &mutex_;
};
#endif // PLCC_THREAD_MUTEX_H_
